﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MIMSDomain.Abstract;
using MIMSDomain.Entities;
using MIMSDomain.Concrete;
using Moq;

namespace MIMSUnitTests.Domain
{
    [TestClass]
    public class EFTribeRepositoryTests
    {
        public IRepository<Tribe> MockTribeRepository;

        [TestInitialize]
        public void Initialize(){
            //Arrange
            var _tribes = new List<Tribe>
            {
                new Tribe{ Id=1, Name="Yoruba"}, new Tribe{ Id=2, Name="Igbo"},
                new Tribe{ Id=3 , Name="Hausa"}
            };
            var target = new Mock<EFTribeRepository>();
            target.Setup(p => p.All).Returns(_tribes.AsQueryable());
            target.Setup(p => p.GetById(It.IsAny<int>())).Returns((int i) =>
                    _tribes.Where(p => p.Id == i).Single());
            target.Setup(p => p.Save(It.IsAny<Tribe>())).Returns((Tribe objTribe) =>
                {
                    if(objTribe.Id.Equals(default(int))){
                        objTribe.Id = _tribes.Count() + 1;
                        _tribes.Add(objTribe);
                    }
                    else{
                        var original = _tribes.Where(p => p.Id == objTribe.Id).Single();
                        if (original == null)
                        {
                            return false;
                        }
                        original.Name = objTribe.Name;
                    }
                    return true;
                });
             target.Setup(p => p.Delete(It.IsAny<Tribe>())).Returns((Tribe objTribe) =>
                {

                    if (_tribes.Contains(objTribe))
                    {
                        _tribes.Remove(objTribe);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                });
            
            MockTribeRepository = target.Object;
        }
        [TestMethod]
        public void CanSaveTribe()
        {
            //Arrange
            var count = MockTribeRepository.All.Count();
            //Assert
            Assert.AreEqual(3, count);
            //Act
            var actual = MockTribeRepository.Save(new Tribe { Name="Ibibio"});
            count = MockTribeRepository.All.Count();
            //Assert
            Assert.AreEqual(4, count);
        }

        [TestMethod]
        public void CanGetTribeById()
        {
            //Act
            var actual = MockTribeRepository.GetById(2);
            //Assert
            Assert.IsNotNull(actual);
            Assert.IsInstanceOfType(actual, typeof(Tribe));
            Assert.AreEqual("Igbo", actual.Name);
        }

        [TestMethod]
        public void CanGetAllTribes()
        {
            //Act
            var actual = MockTribeRepository.All;
            //Assert
            Assert.IsNotNull(actual);
            Assert.IsInstanceOfType(actual, typeof(IQueryable<Tribe>));
            Assert.AreEqual(3,actual.Count());
        }
        
        [TestMethod]
        public void CannotAddDuplicateTribe()
        {
            throw new NotImplementedException();
        }
    }
}
